package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.SalesPackage;

/**
 * @ClassName: SalesPackageRepositoryimpl
 * @Description: TODO
 * @author fanyb
 * @date Nov 4, 2015 9:20:27 AM
 * @version v1.0
 */
public class SalesPackageRepositoryImpl extends BaseQuery{
  
  public PageList findAllByPaging(Pages page) {
    String hqlCount =
        "select count(s.id) from SalesPackage s where 1=1 ";
    String hqlList = " from SalesPackage s where 1=1 ";
    String order = " order by s.createTime desc";
    PageList pl = null;
    try {
      String cod = joinSearchCondition(page);// 拼接查询条件
      pl = queryListByPaging(hqlCount + cod + order, hqlList + cod + order, page);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }
  
  public SalesPackage findByCustomerId(String customerId){
    try{
      String hql="select s from SalesPackage s,Customer c where s.id=c.salesPacakgeId and c.id=:customerId";
      Map<String,Object> paraMap = new HashMap<String,Object>();
      paraMap.put("customerId", customerId);
      List<SalesPackage> salesPackages = queryObject(hql,paraMap);
      if(salesPackages!=null && !salesPackages.isEmpty()){
        return salesPackages.get(0);
      }
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return null;
  }
  
}
